package com.like.sortlist;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.widget.ListView;
import android.widget.TextView;

import com.like.sortlist.SideBar.OnTouchingLetterChangedListener;

public class SortHelper {

	/**
	 * 将数据源排序
	 * @param sorts
	 */
	public static void sortData(List<BaseSort> sorts) {
		SortComparator sortComparator = new SortComparator();
		Collections.sort(sorts, sortComparator);
	}

	/**
	 * 初始化SortList，前提是已经调用sortData方法将数据排过序
	 * @param sideBar
	 * @param dialog
	 * @param adapter
	 * @param list
	 */
	public static void initSortList(SideBar sideBar, TextView dialog,
									final BaseSortAdapter adapter, final ListView list) {
		list.setAdapter(adapter);
		sideBar.setTextView(dialog);
		sideBar.setOnTouchingLetterChangedListener(new OnTouchingLetterChangedListener() {
			@Override
			public void onTouchingLetterChanged(String s) {
				int position = adapter.getPositionForSection(s.charAt(0));
				if (position != -1) {
					list.setSelection(position);
				}
			}
		});
	}

	public static void initSortList(SideBar sideBar, TextView dialog,
									final BaseSortSwipeAdapter adapter, final ListView list) {
		list.setAdapter(adapter);
		sideBar.setTextView(dialog);
		sideBar.setOnTouchingLetterChangedListener(new OnTouchingLetterChangedListener() {
			@Override
			public void onTouchingLetterChanged(String s) {
				int position = adapter.getPositionForSection(s.charAt(0));
				if (position != -1) {
					list.setSelection(position);
				}
			}
		});
	}

	public static <T> List<BaseSort> toBaseSortList(List<T> list) {
		List<BaseSort> baseSorts = new ArrayList<BaseSort>();
		for (int i = 0; i < list.size(); i++) {
			baseSorts.add((BaseSort) list.get(i));
		}
		return baseSorts;
	}
}
